/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu

import cool.taomu.command.DeployCallback
import cool.taomu.config.entity.ConfigEntity
import cool.taomu.monitor.MonitorJvm
import cool.taomu.mqtt.mqttv3.MqttV3Service
import cool.taomu.thread.NetworkThreadServer
import cool.taomu.thread.TaomuThreadPoolFactory
import cool.taomu.utils.yaml.YamlUtils
import java.util.concurrent.Executors
import org.apache.commons.cli.CommandLine
import org.apache.commons.cli.CommandLineParser
import org.apache.commons.cli.DefaultParser
import org.apache.commons.cli.Options

class Taomu {

	def static void main(String[] args) {
		var Options options = new Options();
		options.addOption("c", "config", true, "configure");
		var CommandLineParser parser = new DefaultParser();
		var CommandLine cmd = parser.parse(options, args);
		if (cmd.hasOption("c")) {
			var path = cmd.getOptionValue("c");
			var yml = new YamlUtils(path);
			var config = yml.read(ConfigEntity)
			TaomuThreadPoolFactory.init(config.threadPoolQueueSize, config.rpc.host);
			var coreSize = Runtime.runtime.availableProcessors
			var es = Executors.newFixedThreadPool(coreSize);
			var mqtt = config.mqtt;
			MqttV3Service.subscriber(es, mqtt.host, mqtt.port, mqtt.username, mqtt.password, DeployCallback);
			var m = new MonitorJvm();
			m.start(60, es, config);
			/*NetworkThreadServer.start(
			 * 	config.rpc.host,
			 * 	config.rpc.port,
			 * 	config.rpc.maxThreads,
			 * 	config.rpc.minThreads
			 );*/
			NetworkThreadServer.start(config.rpc.host, config.rpc.port, es,
				config.rpc.selector === null ? coreSize : config.rpc.selector);
		}
	}
}
